package com.qiaofang.collectmanage.service.impl;

import com.qiaofang.collectmanage.dao.OperatorLogEsDao;
import com.qiaofang.collectmanage.dao.elasticsearch.EsClient;
import com.qiaofang.collectmanage.dao.util.ConfigUtils;
import com.qiaofang.collectmanage.service.OperatorLogService;
import com.qiaofang.collectmanage.service.util.BeanUtil;
import com.qiaofang.collectmanage.stub.bean.*;
import com.qiaofang.collectmanage.stub.constants.TableConstants;
import com.qiaofang.common.model.page.PageDTO;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by generator on 2018-8-1 20:37:55
 */
@Service("operatorLogService")
public class OperatorLogServiceImpl implements OperatorLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OperatorLogServiceImpl.class);

    @Autowired
    private OperatorLogEsDao operatorLogEsDao;

    @Override
    public int saveOperatorLog(List<SaveOperatorLogDTO> dtos) {
        return operatorLogEsDao.saveOperatorLog(ConfigUtils.getIndexHswhOperatorLog(), TableConstants.INDEX_TYPE_OPERATOR_LOG, dtos);
    }


    @Override
    public SearchOperatorLogPageAndCountDTO searchOperatorLogPageAndCount(SearchOperatorLogPostDTO searchOperatorLogPostDTO, PageDTO pageDTO) {
        SearchOperatorLogPageAndCountDTO searchOperatorLogPageAndCountDTO = new SearchOperatorLogPageAndCountDTO();
        List<OperatorLogDTO> operatorLogDTOs = new ArrayList<OperatorLogDTO>();
        try {
            QueryBuilder queryBuilder = QueryBuilders.boolQuery();
            if (searchOperatorLogPostDTO.getOperatorType() != null && !"".equals(searchOperatorLogPostDTO.getOperatorType())) {
                ((BoolQueryBuilder) queryBuilder).must(QueryBuilders.termQuery("operatorType", searchOperatorLogPostDTO.getOperatorType()));
            }
            SearchResponse searchResponse = EsClient.queryHouseAgentData(ConfigUtils.getIndexHswhOperatorLog(), TableConstants.INDEX_TYPE_OPERATOR_LOG, queryBuilder, pageDTO.getOffset(), pageDTO.getPageSize());
            LOGGER.info("查询eslticsearch success");
            if (searchResponse != null) {
                SearchHits hits = searchResponse.getHits();
                SearchHit[] hits1 = hits.getHits();
                for (SearchHit hit : hits1) {
                    Map<String, Object> source = hit.getSource();
                    OperatorLogDTO operatorLogDTO = BeanUtil.mapperObj(source, OperatorLogDTO.class);
                    operatorLogDTO.setLogUuid(hit.getId());
                    operatorLogDTOs.add(operatorLogDTO);
                }
                searchOperatorLogPageAndCountDTO.setList(operatorLogDTOs);
                searchOperatorLogPageAndCountDTO.setCount(hits.getTotalHits());
            }

        } catch (Exception e) {
            LOGGER.error("SearchOperatorLogPostDTO=>{}", e);
        }
        return searchOperatorLogPageAndCountDTO;
    }


}
